#define _CRT_SECURE_NO_WARNINGS

#include"Sort.h"

void AdjustDown(int* a, int size, int parent)
{
	int child = (parent * 2) + 1;

	while (child < size)
	{
		if (child + 1 < size && a[child + 1] > a[child])
		{
			child++;
		}

		if (a[parent] < a[child])
		{
			Swap(&a[parent], &a[child]);
		}
		else
		{
			break;
		}

		parent = child;
		child = parent * 2 + 1;
	}

}

void HeapSort(int* a, int size)
{
	int i = 0;
	for (i = (size - 2) / 2; i >= 0; i--)
	{
		AdjustDown(a, size, i);
	}

	for (i = size - 1; i > 0; i--)
	{
		Swap(&a[0], &a[i]);
		AdjustDown(a, i, 0);
	}

}